package com.wondertek.poms.common.utils;

import cn.hutool.core.util.StrUtil;
import com.wondertek.poms.common.constant.Constants;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

public class StringUtil extends StrUtil {

	private StringUtil() {
	}

	public static boolean isNullStr(Object strin) {
		if (strin == null) {
			return true;
		}
		String str = strin.toString();
		boolean flag = false;
		if ("null".equals(str) || "".equals(str) || "".equals(str.trim())) {
			flag = true;
		}
		return flag;
	}

	public static String nullToString(String value) {
		return value != null && !"null".equals(value) ? value.trim() : "";
	}

	public static String nullToString(Object value) {
		return value == null ? "" : value.toString();
	}

	public static Long nullToLong(Object value) {
		return value != null && !"null".equals(value.toString()) ? stringToLong(value.toString().trim()) : 0L;
	}

	public static Long stringToLong(String value) {
		value = nullToString(value);
		Long l;
		if ("".equals(value)) {
			l = 0L;
		} else {
			try {
				l = Long.valueOf(value);
			} catch (Exception var3) {
				l = 0L;
			}
		}

		return l;
	}

	public static Integer nullToInteger(Object value) {
		return value != null && !"null".equals(value.toString()) ? stringToInteger(value.toString()) : 0;
	}

	public static Integer stringToInteger(String value) {
		value = nullToString(value);
		Integer l;
		if ("".equals(value)) {
			l = 0;
		} else {
			try {
				l = Integer.valueOf(value);
			} catch (Exception e) {
				l = 0;
			}
		}

		return l;
	}

	public static Date nullToDate(Object strInput, String dateFormat) {
		if (!isNullStr(strInput)) {
			try {
				SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
				formatter.setLenient(false);
				return formatter.parse(null2Str(strInput));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return null;
	}

	public static String null2Str(Object value) {
		return value != null && !"null".equals(value.toString()) ? value.toString() : "";
	}

	public static String trim(String strin) {
		if (isNullStr(strin)) {
			return null;
		}
		return strin.trim();
	}

	//首字母转小写
	public static String toLowerCaseFirstOne(String s) {
		if (Character.isLowerCase(s.charAt(0))) {
			return s;
		} else {
			return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
		}
	}

	// 首字母转大写
	public static String toUpperCaseFirstOne(String s) {
		if (Character.isUpperCase(s.charAt(0))) {
			return s;
		} else {
			return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
		}
	}

	/**
	 * 得到一个String值的指定长度的字符串形式
	 * NOTE:	不足的前面添'0'
	 *
	 * @param s
	 * @param len
	 * @param cutHead 当s的长度大于len时，截取方式：true,截掉头部；否则从截掉尾部
	 *                例如getStringByAppointLen("12345",3,true) ---> "345"
	 * @return
	 */
	public static String getStringByAppointLen(String s, int len, boolean cutHead) {
		if (s == null || len <= 0) {
			s = "";
		}
		if (len > s.length()) {
			int size = len - s.length();
			StringBuilder sb = new StringBuilder();
			while (size-- > 0) {
				sb.append("0");
			}
			sb.append(s);
			return sb.toString();
		} else if (len == s.length()) {
			return s;
		} else {
			if (cutHead) {
				return s.substring(s.length() - len, s.length());
			} else {
				return s.substring(0, len);
			}
		}
	}

	/**
	 * 得到一个Long值的指定长度的字符串形式
	 *
	 * @param l
	 * @param len
	 * @return
	 */
	public static String getStringByAppointLen(String l, int len) {
		return getStringByAppointLen(l, len, true);
	}


	/**
	 * 功能描述: 〈下划线转驼峰〉
	 *
	 * @param para 1
	 * @return : java.lang.String
	 * @author : yangyu 2019/12/6 13:40
	 */
	public static String underlineToHump(String para) {
		StringBuilder result = new StringBuilder();
		String[] a = para.split("_");
		for (String s : a) {
			if (!para.contains("_")) {
				result.append(s);
				continue;
			}
			if (result.length() == 0) {
				result.append(s.toLowerCase());
			} else {
				result.append(s.substring(0, 1).toUpperCase());
				result.append(s.substring(1).toLowerCase());
			}
		}
		return result.toString();
	}

	/**
	 * 功能描述: 〈驼峰转下划线〉
	 *
	 * @param para 1
	 * @return : java.lang.String
	 * @author : yangyu 2019/12/6 13:42
	 */
	public static String humpToUnderline(String para) {
		StringBuilder sb = new StringBuilder(para);
		int temp = 0;//定位
		if (!para.contains("_")) {
			for (int i = 0; i < para.length(); i++) {
				if (Character.isUpperCase(para.charAt(i))) {
					sb.insert(i + temp, "_");
					temp += 1;
				}
			}
		}
		return sb.toString().toUpperCase();
	}

	/***
	 * @name: strRemoval
	 * @description: 字符串去重
	 * @params: [conjunction, str]
	 * @return: java.lang.String
	 * @date: 2020/3/11 14:37
	 * @auther: ma
	 **/
	public static String noRepeat(String str) {
		return noRepeat(str, Constants.DEFAULT_SEPARATOR);
	}

	public static String noRepeat(String str, CharSequence separator) {
		return isBlank(str) ? null :
				join(separator, new HashSet<String>(
						splitTrim(str, separator)));
	}

	public static List<String> noRepeatToList(String str, CharSequence separator) {
		if (isBlank(str)) return null;
		HashSet<String> hashSet = new HashSet<String>(
				splitTrim(str, separator));
		return new ArrayList<String>(hashSet);
	}

	/**
	 * 按照英文逗号切分字符串，去除切分后每个元素两边的空白符，去除空白项
	 *
	 * @param str 被切分的字符串
	 * @return 切分后的集合
	 * @since 3.2.0
	 */
	public static List<String> splitTrim(CharSequence str) {
		return splitTrim(str, Constants.DEFAULT_SEPARATOR, -1);
	}

	public static String null2Str(String value) {
		return value != null && !"null".equals(value) ? value.trim() : "";
	}

}
